<!--
 * @Author: hq
 * @Date: 2022-05-18 19:57:54
 * @LastEditors: hq
 * @LastEditTime: 2022-07-13 15:19:26
 * @Description: 权限规则
 * @version: 1.0
-->
<template>
  <div class="info" v-loading="loading">
    <div class="card_con">
      <div class="row mb20">
        <div class="l" @click="handleAdd">
          <i class="el-icon-circle-plus-outline"></i>添加公告
        </div>
      </div>
      <el-table
        highlight-current-row
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        :header-cell-style="{
          'text-align': 'left',
        }"
        :cell-style="{ 'text-align': 'left' }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="id" label="公告id" min-width="20%">
        </el-table-column>
        <el-table-column prop="title" label="标题" min-width="20%">
        </el-table-column>
        <el-table-column prop="from" label="发布人" min-width="20%">
        </el-table-column>
        <el-table-column prop="abst" label="摘要" min-width="20%">
        </el-table-column>
        <el-table-column prop="time" label="发布时间" min-width="20%">
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="20%">
        </el-table-column>
        <el-table-column prop="status" label="开启" min-width="15%">
          <template slot-scope="scope">
            <div v-if="scope.row.status === 0" class="red">禁用</div>
            <div v-if="scope.row.status === 1" class="green">启用</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="25%">
          <template slot-scope="scope">
            <div class="caozuo">
              <div class="handleEdit hand_cs" @click="handleEdit(scope.row)">
                修改
              </div>
              <div
                class="handleDelete hand_cs"
                @click="handleDelete(scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :page="page" @onCurrentChange="handleCurrentChange" />
      </div>
    </div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="addlog"
      width="800px"
    >
      <EditDialog
        v-if="addlog"
        :rowData="rowData"
        @handleClose="handleClose"
      ></EditDialog>
    </el-dialog>
  </div>
</template>

<script>
import { noticeList, delAnnounce } from "@/api/configure";
import EditDialog from "./add_edit_dialog.vue";
import Pagination from "@/components/Pagination.vue";
export default {
  components: { Pagination, EditDialog },
  data() {
    return {
      loading: true,
      title: "",
      tableData: [],
      page: {
        total: 0,
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页的条数
      },
      addlog: false,
      rowData: {},
    };
  },
  methods: {
    async noticeList() {
      const { data, err } = await noticeList({});
      if (err === 0) {
        this.loading = false;
        this.tableData = data.data;
        this.page.total = data.total;
        this.page.pageSize = data.per_page;
      }
    },
    //删除
    async delAnnounce(id) {
      const { data, err } = await delAnnounce({
        id,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.noticeList();
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //新增
    handleAdd() {
      this.title = "添加公告";
      this.addlog = true;
      this.rowData = {};
    },
    //编辑
    handleEdit(row) {
      this.title = "编辑公告";
      this.addlog = true;
      this.rowData = row;
    },
    //删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delAnnounce(row.id);
        })
        .catch(() => {});
    },
    handleClose() {
      this.addlog = false;
      this.noticeList();
    },
    handleCurrentChange(val) {
      this.noticeList();
    },
  },
  created() {
    this.noticeList();
  },
};
</script>

<style lang="scss" scoped>
@import "@/style/index.scss";
.slt {
  justify-content: flex-start !important;
}
.row {
  display: flex;
  justify-content: center;
  border-radius: 2px;
  border: 1px solid #ccc;
  width: 100px;
  .l {
    width: 80px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;

    i {
      margin-right: 6px;
      font-size: 14px;
    }
    &:hover {
      cursor: pointer;
    }
  }
  .l:nth-child(3) {
    border-right: 0;
  }
}
.caozuo {
  display: flex;
  justify-content: flex-start !important;
  .handleDelete {
    margin-left: 20px;
  }
  .handleEdit {
    margin-left: 20px;
  }
  .handleAdd {
    color: $color-primary2;
  }
}
.info {
  .title {
    font-size: 16px;
    margin-bottom: 13px;
  }
  .tab {
    background-color: #fff;
    padding: 20px;
    box-sizing: border-box;
  }
}
.tab {
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;
  .el-input {
    width: 20%;
    margin-right: 20px;
  }
  .el-button {
    margin-left: 20px;
  }
}

/deep/.el-breadcrumb__inner {
  cursor: pointer !important;
}
.marl {
  padding-left: 2px;
}
.midle_tb {
  margin-top: 20px;
}
/deep/.el-date-editor {
  width: 100%;
}
.el-select {
  width: 100%;
}
.caozuo {
  display: flex;
  justify-content: center;
  .handleDelete {
    margin-left: 20px;
  }
}
/deep/.el-dialog {
  height: 78%;
  .el-dialog__body {
    height: 90%;
    overflow: auto;
  }
}
</style>
